package com.card.client.dialog;

import com.card.client.entry.CreditCard;
import com.card.client.entry.Util;
import com.card.client.service.CreditCardService;
import com.card.client.service.CreditCardServiceAsync;
import com.card.client.view.CreditCardView;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class EditCreditCardDialog extends DialogBox {

	private static DialogUiBinder uiBinder = GWT.create(DialogUiBinder.class);

	interface DialogUiBinder extends UiBinder<Widget, EditCreditCardDialog> {
	}

	private final CreditCardServiceAsync ccs = GWT.create(CreditCardService.class);
	
	private Long id;
	
	@UiField Button submit;
	
	@UiField Button closeDialog;
	
	@UiField TextBox name;
	
	@UiField TextBox item;
	
	@UiField TextBox amount;
	
	@UiField ListBox billDate;
	
	@UiField Label message;
	
	CreditCardView creditCardView;
	
	CreditCard card;

	public EditCreditCardDialog(String title, String descript, CreditCardView creditCardView, CreditCard card, int flag) {
		setWidget(uiBinder.createAndBindUi(this));
		setTitle(title);
		setText(descript);
		setGlassEnabled(true);
		setAnimationEnabled(true);
		setModal(true);
		message.setStyleName("descript");
		name.setMaxLength(Util.INPUT_TEXT_MAX_LENGTH);
		item.setMaxLength(Util.INPUT_TEXT_MAX_LENGTH);
		amount.setMaxLength(Util.INPUT_TEXT_MAX_LENGTH);
		center();
		billDate.addItem("", "0");
		for(int i=1;i<32;i++){
			String day = Integer.toString(i);
			billDate.addItem("每月" + day + "日", day);
		}
		this.creditCardView = creditCardView;
		submit.setText("添加");
		if(flag == Util.DIALOG_CONTROL_EDIT){
			submit.setText("修改");
			this.card = card;
			id = card.getId();
			name.setText(card.getName());
			item.setText(card.getItem());
			amount.setText(Util.intToStr(card.getAmount()));
			int value = Integer.parseInt(card.getBillDate());
			billDate.setSelectedIndex(value);
		}
		int w = amount.getOffsetWidth();
		billDate.setWidth(w + "px");
	}

	@UiHandler("submit")
	void onClickSubmit(ClickEvent e) {
		if(!isValid()){
			return;
		}
		CreditCard cc = new CreditCard();
		cc.setId(id);
		cc.setName(name.getText());
		cc.setItem(item.getText());
		cc.setAmount(Util.strToInt(amount.getText()));
		cc.setBillDate(billDate.getValue(billDate.getSelectedIndex()));
		if(null != id){
			ccs.updateCreditCard(cc, new AsyncCallback<Void>() {
				public void onSuccess(Void result) {
					hide();
					creditCardView.onLoad();
				}
				public void onFailure(Throwable caught) {}
				
			});
		} else {
			ccs.addCreditCard(cc, new AsyncCallback<Void>() {
				public void onSuccess(Void result) {
					hide();
					creditCardView.onLoad();
				}
				public void onFailure(Throwable caught) {}
			});
		}
	}
	
	@UiHandler("closeDialog")
	void onClickClose(ClickEvent e) {
		hide();
		creditCardView.onLoad();
	}

	private void setMessage(String msg){
		message.setText(msg);
	}

	private boolean isValid(){
		setMessage("");
		String check = Util.validInput(name.getText());
		if(null != check){
			name.setFocus(true);
			setMessage("名称" + check);
			return false;
		}
		check = Util.validInput(amount.getText());
		if(null != check){
			amount.setFocus(true);
			setMessage("标识" + check);
			return false;
		}
		check = Util.validAmount(amount.getText());
		if(null != check){
			amount.setFocus(true);
			setMessage(check);
			return false;
		}
		int index = billDate.getSelectedIndex();
		String value = billDate.getValue(index);
		if("0".equals(value)){
			billDate.setFocus(true);
			setMessage("没有选择出账日");
			return false;
		}
		return true;
	}
}
